package com.pervasive.geoTagging;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.view.View;

public class MainActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        Editor edit = pref.edit();
        if(!pref.contains("marker"))
        	edit.putString("marker", "Rosso").commit();
        if(!pref.contains("Flash"))
        	edit.putString("Flash", "Auto").commit();
        if(!pref.contains("Quality"))
        	edit.putString("Quality", "Media").commit();
        if(!pref.contains("IP"))
        	edit.putString("IP", "localhost").commit();
    }

	public void onResume(){
		super.onResume();
        createDirs();
	}
	
	public void onBackPressed(){
		super.onBackPressed();
		deleteDirs();
		System.gc();
		finish();
	}
	
	private static void deleteDirs(){
		try{
			if(PHOTOS_DIR.exists())
				delete(PHOTOS_DIR);
			if(NOTES_DIR.exists())
				delete(NOTES_DIR);
		}catch(Exception e){ }
	}
	
	private static void delete(File f){
		if(f.isDirectory())
			for(File temp: f.listFiles())
				delete(temp);
		f.delete();
	}
	
	protected static void createDirs(){
		if(!DIR_APP.exists())
			DIR_APP.mkdir();
		if(!DIR_OFF.exists())
			DIR_OFF.mkdir();
		deleteDirs();
		PHOTOS_DIR = new File(DIR_APP.getAbsolutePath()+"/"+PHOTOS_DIR_NAME);
		PHOTOS_DIR.mkdir();
       	NOTES_DIR = new File(DIR_APP.getAbsolutePath()+"/"+NOTES_DIR_NAME);
       	NOTES_DIR.mkdir();
	}
    
    public void click(View view){
    	int code = view.getId();
    	Intent i = null;
    	switch(code){
	    	case R.id.buttonMap: 
	    		i = new Intent(this, MappaActivity.class);
	    		break;
	    	case R.id.buttonCamera:
	    		i = new Intent(this, CustomCameraActivity.class);
	    		break;
	    	case R.id.buttonPreferences:
	    		i = new Intent(this, PreferencesActivity.class);
    	}
    	startActivity(i);
    }
    
    protected static final String DIR_APP_NAME = "geoTagging folder", PHOTOS_DIR_NAME = "photos", NOTES_DIR_NAME = "notes";
    protected static SharedPreferences pref;
    protected static File PHOTOS_DIR, NOTES_DIR;
    protected static final File DIR_APP = new File(Environment.getExternalStorageDirectory().toString()+"/"+DIR_APP_NAME);
    protected static final File DIR_OFF = new File (Environment.getExternalStorageDirectory().toString() +"/offLinePhoto");
}